package com.wlhse.service.impl;

import com.github.pagehelper.PageHelper;
import com.wlhse.dao.ProblemSourceDao;
import com.wlhse.entity.ProblemSourcePojo;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.ProblemSourceService;
import com.wlhse.util.SortCodeUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ProblemSourceServiceImpl implements ProblemSourceService {

    @Resource
    private ProblemSourceDao problemSourceDao;

    @Resource
    private SortCodeUtil sortCodeUtil;

    // 查全部-分页
    @Override
    public String listProblemSource(ProblemSourcePojo pojo) {
        //查询所有
        if (pojo.getType().equals("all"))
            return NR.r(problemSourceDao.queryProblemSource(pojo));
        int total = problemSourceDao.queryTotal();
        PageHelper.startPage(pojo.getPageIdx(), pojo.getPageSize());
        List<ProblemSourcePojo> list = problemSourceDao.queryProblemSource(pojo);
        return NR.r(list, total, pojo.getPageIdx());
    }

    //查 By Id
    @Override
    public String queryProblemSourceyId(int problemSourceId) {
        return NR.r(problemSourceDao.queryProblemSourceyId(problemSourceId));
    }

    @Override
    @Transactional
    public String updateProblemSource(ProblemSourcePojo pojo) {
        if (problemSourceDao.updateProblemSource(pojo) <= 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String removeProblemSource(int problemSourceId) {
        if (problemSourceDao.deleteProblemSource(problemSourceId) <= 0)
            throw new WLHSException("删除失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String insertProblemSource(ProblemSourcePojo pojo) {
        String maxString = StringUtils.isNotBlank(problemSourceDao.getMaxString()) ?
                sortCodeUtil.getMaxCodeString(problemSourceDao.getMaxString()) : "0001";
        pojo.setProblemSourceCode(maxString);
        if (problemSourceDao.insertProblemSource(pojo) <= 0)
            throw new WLHSException("新增失败");
        return NR.r();
    }

    @Override
    public String queryProblemSourceByVagueCondition(String keyword) {
        return NR.r(problemSourceDao.queryProblemSourceByVagueCondition(keyword));
    }


    @Override
    public String queryProblemSourceBySourceName(ProblemSourcePojo problemSourcePojo) {
        List<ProblemSourcePojo> dataList = problemSourceDao.queryProblemSourceBySourceName(problemSourcePojo.getProblemSourceName());
        int total = problemSourceDao.queryTotal2(problemSourcePojo.getProblemSourceName());
        PageHelper.startPage(problemSourcePojo.getPageIdx(), problemSourcePojo.getPageSize());
        return NR.r(dataList, total, problemSourcePojo.getPageIdx());
    }

}
